#include "mythread.h"
#include <QDebug>

MyThread::MyThread(QThread *parent) : QThread(parent)
{

    connect(this,SIGNAL(finished()),this,SLOT(deleteLater())); //支持自动释放资源 https://blog.csdn.net/qq_14945437/article/details/98868606
    /**
2021-08-10 15:11:50:331[7f2dc67aa700]WARN: QObject: Cannot create children for a parent that is in a different thread.
(Parent is QProcess(0x13466a0), parent's thread is QThread(0x1304480), current thread is MyThread(0x1349cf0) ((null):0, (null))
 */
//    ce = new CommandExecutor("ls /");
//    connect(ce, SIGNAL(finishedEvent()), this, SLOT(onFinished()));
    qDebug() << "init MyThread";
}

MyThread::~MyThread()
{
    qDebug() << "~";
    if(ce){
        ce->deleteLater();
    }
}

void MyThread::run()
{
    qDebug() << "start thread";
    ce = new CommandExecutor("ls /");
    connect(ce, SIGNAL(finishedEvent()), this, SLOT(onFinished()));
    ce->start();
    qDebug() << "exec";
    exec();
    qDebug() << "exec finished";
}

void MyThread::onFinished()
{
    qDebug() << "onFinished";
}
